package com.example.home.controller;

import com.example.home.Dao.StudentRepository;
import com.example.home.Dao.TeacherRepository;
import com.example.home.Entities.Student;
import com.example.home.Entities.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("teachers")
public class TeacherController {

    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    StudentRepository studentRepository;

    @GetMapping
    public String outStudentForm1(Model model){
        List<Teacher> teacher1 =teacherRepository.findAll();
        model.addAttribute("teacher2",teacher1);
        return "teacher/list-teacherOne";
    }

    @GetMapping("/new")
    public String outStudentForm(Model model){
        Teacher teacher = new Teacher();
        model.addAttribute("teachOr",teacher);
        return "teacher/Teacher";
    }

    @PostMapping("/save")
    public String CreateStudent(Model model, Teacher teacher){

        //处理保存数据库的行为
       teacherRepository.save(teacher);

        //使用定位防止重复提交
        return "redirect:/teachers/new";

    }

}
